package com.singzi.leetcode.N1000;

public class T0997 {
    public static void main(String[] args) {
        Solution0097 solution0097 = new Solution0097();
        int [][]trust = {{1,2}};
        System.out.println(solution0097.findJudge(2, trust));
    }
}
class Solution0097 {
    public int findJudge(int n, int[][] trust) {
        int []trust1 = new int[n];
        int []trust2 = new int[n];
        for (int[] nums : trust) {
            trust1[nums[0]-1]++;
        }
        int index = trust1(trust1);

        if(index > -1){
            for (int[] nums : trust) {
                if(nums[1]==index+1)
                trust2[nums[0]-1]++;
            }
            if(trust2(trust2, index+1)){
                return index+1;
            }
            else return -1;
        }
        else {
            return -1;
        }
    }
    public int trust1(int []trust1 ){
        int count = 0;
        int index = 0;
        for (int i = 0; i < trust1.length; i++) {
            if(trust1[i] == 0){
                count++;
                index = i;
            }
        }
        if(count == 1) return index;
        else return -1;

    }
    public boolean trust2(int []trust2, int index){
        for (int i = 0; i < trust2.length; i++) {
            if(i == index-1) continue;
            if(trust2[i]==0) return false;
        }
        return true;

    }

}